<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>资源管理</title>
<meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta charset="utf-8">
  <link rel="stylesheet" href="../layui/css/layui.css"  media="all">
  <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>

<body>
<form class="layui-form">
	<div class="layui-form" id="list_page" style="margin-top:30px;">
	    <div class="layui-inline">
		    <label class="layui-form-label">资源编号：</label>
		    <div class="layui-input-inline">
			    <input class="layui-input" type = "text" id="did" >
		</div>

	    <div class="layui-inline" style="margin-right:10px;">
			<button type="button" class="layui-btn search_btn" data-type="reload">查询</button>
	</div>
 	 </div>
 	 <div style="margin-left:30px; margin-right:30px;">
 	 	<table class="layui-table" id = "resourcelist" lay-filter="resourcelist"></table>
 	 </div>
</form>
</body>
<script src="../js/jquery-3.2.1.min.js"></script>
<script src="../js/vue.min.js"></script>
<script src="../layui/layui.js"></script>
<script>
layui.use('table', function() {
	var table = layui.table;
	table.render({
		elem : '#resourcelist',
	    height:430,
		page: true,
		limit: 8,
		url : '/xueyou/queryresources.action',
		response: {
			statusName: 'code'   //规定数据状态的字段名称，默认：code
			,statusCode: 0       //规定成功的状态码，默认：0
			,msgName:   'msg'    //规定状态信息的字段名称，默认：msg
			,countName: 'count'  //规定数据总数的字段名称，默认：count
			,dataName:  'data'   //规定数据列表的字段名称，默认：data
		},
		cols: [[ //标题栏
		    {field: 'did', title: '资源编号', align:'center'}
		   ,{field: 'id', title: '用户账号', align:'center'}
		   ,{field: 'flag', title: '资源标记', align:'center'}
		   ,{field: 'filename', title: '资源文件名', align:'center'}
		   ,{title : '操作', toolbar: '#caozuo', align: 'center'}
		]],
		id : "resourcelistTable"
    });	
	 
	table.on('tool(resourcelist)', function(obj){
		  var data = obj.data;
		  
		  if(obj.event === 'edit'){
			  layer.confirm('真的修改did为['+ data.did +']的状态么？', function(index){
					Edit(data,index,obj);	
				} );			    
		  }
		  if(obj.event === 'del'){
				layer.confirm('真的删除did为['+ data.did +']的资源么？', function(index){
					Deluser(data,index,obj);
				} );

			}

	});
	
	//编辑功能 ，只对资源状态进行编辑
	function Edit(data,index,obj){
		$.ajax({
			url:"/xueyou/modify_resource.action",
			data:{"did":data.did, "flag":data.flag},
			dataType:"json",
			success: function(data){
				
				if(data.code!=1){

					layer.close(index);
					obj.update({
						flag: data.flag
					});
					layer.msg("修改成功");
				}else{
					layer.msg("修改失败");
					
				}
				
			}
		});
	}
//删除用户	
	function Deluser(data,index,obj){
		$.ajax({
			url: "/xueyou/resource_del.action",    //这个是后台的路由地址
			data:{"did":data.did},//传给后台的值
			dataType: "json",
			success: function(data){
				if(data.code != 1){   //从前台取回的状态值
					layer.close(index);
					//同步更新表格和缓存对应的值
					obj.del();
					layer.msg("删除成功");
				}else{
					layer.msg(data.msg	);
				}
			}
		});

	}

//表格重载
	var $ = layui.$, active = {
            reload: function(){
                var demoReload = $('#did');
                //执行重载
                table.reload('resourcelistTable', {
                    page: {

                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                        did: demoReload.val()
                    }
                });
            }
        };


});

$(".search_btn").on("click",function(){
	var table = layui.table;
	table.reload("resourcelistTable",{
		page: {
			curr: 1 //重新从第 1 页开始
		},
		where:{
			did:  $('#did').val()
		}
	});

table.render({
	elem : '#resourcelist',
   	height:430,
	page: true,
	limit: 10,
	url : '/xueyou/query_resource.action',
	where:{
		did:  $('#did').val()
	},
	response: {
		statusName: 'code'   //规定数据状态的字段名称，默认：code
		,statusCode: 0       //规定成功的状态码，默认：0
		,msgName:   'msg'    //规定状态信息的字段名称，默认：msg
		,countName: 'count'  //规定数据总数的字段名称，默认：count
		,dataName:  'data'   //规定数据列表的字段名称，默认：data
	},
	cols: [[ //标题栏
	    {field: 'did', title: '资源编号', align:'center'}
		   ,{field: 'id', title: '用户账号', align:'center'}
		   ,{field: 'flag', title: '资源标记', align:'center'}
		   ,{field: 'filename', title: '资源文件名', align:'center'}
		   ,{title : '操作', toolbar: '#caozuo', align: 'center'}
	]],
	id : "resourcelistTable"
});

});



</script>
<script type="text/html" id="caozuo">
<a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

</html>